package utility

import (
	"context"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/pkg/errors"
	"github.com/shopspring/decimal"
)

type uMath struct {
}

var Math = uMath{}

func (u *uMath) isNumber(num string) error {
	if !gstr.IsNumeric(num) {
		return errors.New("input non-numeric string")
	}
	return nil
}

func (u *uMath) Sub(ctx context.Context, l, r string) (string, error) {
	if err := u.isNumber(l); err != nil {
		return "", err
	}
	if err := u.isNumber(r); err != nil {
		return "", err
	}
	lNum, _ := decimal.NewFromString(l)
	rNum, _ := decimal.NewFromString(r)
	return lNum.Sub(rNum).String(), nil
}
